    //
//  LayerGeometry.m
//  LayerStudy
//
//  Created by longhuihu on 11-6-29.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "LayerGeometryController.h"
#import <QuartzCore/CALayer.h>

@interface MyDelegate : NSObject
@end

@implementation MyDelegate

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
{
	CGPoint point = layer.anchorPoint;
	CGRect anchorRect = CGRectMake(layer.bounds.size.width*point.x-3, 
								   layer.bounds.size.height*point.y-3, 6, 6);
	const CGFloat color[] = {1,0,0,1};
	CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
	CGContextSetFillColorSpace(ctx,space);
	CFRelease(space);
	CGContextSetFillColor(ctx, color);
	CGContextFillEllipseInRect(ctx, anchorRect);
	
	NSString *frame =[NSString stringWithFormat:@"frame:%g,%g,%g,%g",layer.frame.origin.x,
					   layer.frame.origin.y,
					   layer.frame.size.width,
					   layer.frame.size.height];
	NSString *bounds =[NSString stringWithFormat:@"bounds:%g,%g,%g,%g",layer.bounds.origin.x,
					  layer.bounds.origin.y,
					  layer.bounds.size.width,
					  layer.bounds.size.height];
	NSString *anchor =[NSString stringWithFormat:@"anchor:%g,%g",layer.anchorPoint.x,
					   layer.anchorPoint.y];
	NSString *position =[NSString stringWithFormat:@"position:%g,%g",layer.position.x,
					  layer.position.y];

	CGAffineTransform transform = CGAffineTransformMakeScale(1,-1);
	CGContextSetTextMatrix(ctx, transform);
	CGContextSelectFont(ctx, "Helvetica", 15, NSASCIIStringEncoding);
	
	const CGFloat color2[] = {0,0,0,1};
	CGContextSetFillColor(ctx, color2);
	
	const char* s =[frame cStringUsingEncoding:NSASCIIStringEncoding];
	CGContextShowTextAtPoint(ctx, 0,15,s,strlen(s));
	s =[bounds cStringUsingEncoding:NSASCIIStringEncoding];
	CGContextShowTextAtPoint(ctx, 0,15*2,s,strlen(s));
	s =[anchor cStringUsingEncoding:NSASCIIStringEncoding];
	CGContextShowTextAtPoint(ctx, 0,15*3,s,strlen(s));
	s =[position cStringUsingEncoding:NSASCIIStringEncoding];
	CGContextShowTextAtPoint(ctx, 0,15*4,s,strlen(s));
}

@end


@implementation LayerGeometryController

// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/
-(void)sliderValueChanged:(id)sender
{
	if (sender==sliderX) {
		CGPoint anchor = demoLayer.anchorPoint;
		anchor.x = sliderX.value;
		demoLayer.anchorPoint = anchor;
		[demoLayer setNeedsDisplay];
	} else if (sender==sliderScale) {
		demoLayer.transform = CATransform3DMakeScale(2*sliderScale.value, 2*sliderScale.value, 1);
		[demoLayer setNeedsDisplay];
	}
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	demoLayer = [[[CALayer alloc]  init] autorelease];
	demoLayer.frame = CGRectMake(50, 100, 200, 100);
	demoLayer.delegate = [[MyDelegate alloc] init];
	demoLayer.backgroundColor = [[UIColor colorWithRed:0.5 green:0.5 blue:0.8 alpha:0.8] CGColor];
	[self.view.layer addSublayer:demoLayer];
	[demoLayer setNeedsDisplay];
	
	UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(5, 300, 70,30)];
	[self.view addSubview:label];
	label.text = @"anchor X";
	
	sliderX = [[UISlider alloc] initWithFrame:CGRectMake(80, 300, 240, 30)];
	[sliderX setMinimumValue:0];
	[sliderX setMaximumValue:1];
	[sliderX addTarget:self action:@selector(sliderValueChanged:) forControlEvents:UIControlEventValueChanged];
	sliderX.value =0.5;
	[self.view addSubview:sliderX];
	
	label = [[UILabel alloc] initWithFrame:CGRectMake(5, 330, 70,30)];
	[self.view addSubview:label];
	label.text = @"scale";
	
	sliderScale = [[UISlider alloc] initWithFrame:CGRectMake(80, 330, 240, 30)];
	[sliderScale setMinimumValue:0];
	[sliderScale setMaximumValue:1];
	[sliderScale addTarget:self action:@selector(sliderValueChanged:) forControlEvents:UIControlEventValueChanged];
	sliderScale.value =0.5;
	[self.view addSubview:sliderScale];
}

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
	[demoLayer release];
	demoLayer = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end
